import { reactive, onMounted, onBeforeUnmount, getCurrentInstance } from 'vue';
import page from './page.ts';
export function useListComponent() {
  const { proxy } = getCurrentInstance() as any;
  //页面信息
  const dataInfo: any = reactive({
    loading: false, //加载
    loadingMsg: '列表数据加载中', //加载
    table: {
      title: '物料库存数据',
      tbody: [],
      thead: [
        {
          label: '操作时间',
          prop: 'createTime',
          align: 'center',
          width: 170,
        },
        {
          label: '操作人员',
          prop: 'creatorName',
          align: 'center',
          width: 80,
        },
        {
          label: '操作状态',
          prop: 'changeType',
          align: 'center',
          width: 100,
        },
        {
          label: '关联单号',
          prop: 'associationCode',
          align: 'center',
          width: 150,
        },
        {
          label: '生产批次',
          prop: 'productionBatchNumber',
          align: 'center',
          width: 100,
        },
        {
          label: '生产日期',
          prop: 'productionDate',
          align: 'center',
          width: 100,
        },
        {
          label: '库区库位',
          prop: 'warehouseInfo',
          align: 'center',
          width: 100,
        },
        {
          label: '变更前库存',
          prop: 'afterStockQuantity1',
          align: 'center',
          width: 100,
        },
        {
          label: '变更数量',
          prop: 'changeQuantiy',
          align: 'center',
          width: 100,
        },
        {
          label: '变更后库存',
          prop: 'afterStockQuantity',
          align: 'center',
          width: 100,
        },
        {
          label: '备注',
          prop: 'remark',
          align: 'left',
          width: null,
        },
      ],
      //表格-按钮数据
      btns: [
        {
          title: '导出全部数据',
          click: () => {
            setLoading('正在导出全部数据,请稍等');
            exportBtn(dataInfo.table.tbody);
          },
        },
      ],
    },
    search: {
      beginTime: '2025-07-26', //开始日期
      endTime: '2025-08-25', //结束日期
    },
    good: {},
    closingBalance: {}, //结存信息
    closingBalanceThead: [
      {
        label: '期初',
        prop: '',
        align: 'center',
        width: null,
        children: [
          {
            label: '数量',
            prop: 'initialQuantity',
            align: 'center',
            width: 100,
          },
          {
            label: '未税',
            prop: 'initialSubtotalBeforeTax',
            align: 'center',
            width: 100,
          },
          {
            label: '含税',
            prop: 'initialSubtotal',
            align: 'center',
            width: 100,
          },
        ],
      },
      {
        label: '当月采购',
        prop: '',
        align: 'center',
        width: null,
        children: [
          {
            label: '数量',
            prop: 'nowPurchaseQuantity',
            align: 'center',
            width: 100,
          },
          {
            label: '未税',
            prop: 'nowPurchaseSubtotalBeforeTax',
            align: 'center',
            width: 100,
          },
          {
            label: '含税',
            prop: 'nowPurchaseSubtotal',
            align: 'center',
            width: 100,
          },
        ],
      },
      {
        label: '实际领料',
        prop: '',
        align: 'center',
        width: null,
        children: [
          {
            label: '数量',
            prop: 'nowGetProduceMaterialQuantity',
            align: 'center',
            width: 100,
          },
          {
            label: '未税',
            prop: 'nowGetProduceMaterialSubtotalBeforeTax',
            align: 'center',
            width: 100,
          },
          {
            label: '含税',
            prop: 'nowGetProduceMaterialSubtotal',
            align: 'center',
            width: 100,
          },
        ],
      },
      {
        label: '当月退货',
        prop: '',
        align: 'center',
        width: null,
        children: [
          {
            label: '数量',
            prop: 'nowStockGoodsCreditQuantity',
            align: 'center',
            width: 100,
          },
          {
            label: '未税',
            prop: 'nowStockGoodsCreditSubtotalBeforeTax',
            align: 'center',
            width: 100,
          },
          {
            label: '含税',
            prop: 'nowStockGoodsCreditSubtotal',
            align: 'center',
            width: 100,
          },
        ],
      },
      {
        label: '当月盘盈',
        prop: '',
        align: 'center',
        width: null,
        children: [
          {
            label: '数量',
            prop: 'nowTakeStockProfitQuantity',
            align: 'center',
            width: 100,
          },
          {
            label: '未税',
            prop: 'nowTakeStockProfitSubtotalBeforeTax',
            align: 'center',
            width: 100,
          },
          {
            label: '含税',
            prop: 'nowTakeStockProfitSubtotal',
            align: 'center',
            width: 100,
          },
        ],
      },
      {
        label: '当月盘亏',
        prop: '',
        align: 'center',
        width: null,
        children: [
          {
            label: '数量',
            prop: 'nowTakeStockLossQuantity',
            align: 'center',
            width: 100,
          },
          {
            label: '未税',
            prop: 'nowTakeStockLossSubtotalBeforeTax',
            align: 'center',
            width: 100,
          },
          {
            label: '含税',
            prop: 'nowTakeStockLossSubtotal',
            align: 'center',
            width: 100,
          },
        ],
      },
      {
        label: '其他入库',
        prop: '',
        align: 'center',
        width: null,
        children: [
          {
            label: '数量',
            prop: 'nowOtherInQuantity',
            align: 'center',
            width: 100,
          },
          {
            label: '未税',
            prop: 'nowOtherInSubtotalBeforeTax',
            align: 'center',
            width: 100,
          },
          {
            label: '含税',
            prop: 'nowOtherInSubtotal',
            align: 'center',
            width: 100,
          },
        ],
      },
      {
        label: '其他出库',
        prop: '',
        align: 'center',
        width: null,
        children: [
          {
            label: '数量',
            prop: 'nowOtherOutQuantity',
            align: 'center',
            width: 100,
          },
          {
            label: '未税',
            prop: 'nowOtherOutSubtotalBeforeTax',
            align: 'center',
            width: 100,
          },
          {
            label: '含税',
            prop: 'nowOtherOutSubtotal',
            align: 'center',
            width: 100,
          },
        ],
      },
      {
        label: '期末',
        prop: '',
        align: 'center',
        width: null,
        children: [
          {
            label: '数量',
            prop: 'finalQuantity',
            align: 'center',
            width: 100,
          },
          {
            label: '未税',
            prop: 'finalSubtotalBeforeTax',
            align: 'center',
            width: 100,
          },
          {
            label: '含税',
            prop: 'finalSubtotal',
            align: 'center',
            width: 100,
          },
        ],
      },
    ], //结存信息
    goodThead: [
      {
        label: '物料编码',
        prop: 'goodsCode',
      },
      {
        label: '规格编码',
        prop: 'specificationCode',
      },
      {
        label: '物料名称',
        prop: 'goodsName',
      },
      {
        label: '规格名称',
        prop: 'specificationName',
      },
      {
        label: '供应商',
        prop: 'supplierName',
      },
      {
        label: '物料分组',
        prop: 'goodsGroupName',
      },
      {
        label: '转换比例',
        prop: 'specificationJsValue',
      },
      {
        label: '税率',
        prop: 'taxRate',
      },
      {
        label: '采购单价',
        prop: 'goodsPrice',
        unit: '元',
      },
      {
        label: '含税均价',
        prop: 'averagePrice',
        unit: '元',
      },
      {
        label: '未税均价',
        prop: 'averagePriceBeforeTax',
        unit: '元',
      },
    ],
  });
  // 选择物料
  const selectGoodBtn = (arr: any) => {
    const _row = arr[0] || {};
    proxy.$api.Goods.GetGoodsSpecificationViewDetail({
      queryBy: 1,
      queryValue: _row.specificationCode,
    })
      .then((res: any) => {
        proxy.$message('获取成功');
        dataInfo.good = res;
        getList(res);
      })
      .catch(() => {});
  };
  const getList = (good: any = dataInfo.good) => {
    getPageQueryStockBalancesReport(good);
    getGetPageStockGoodsLog(good);
  };
  // 获取结存数据
  const getPageQueryStockBalancesReport = (good: any) => {
    proxy.$api.Report.PageQueryStockBalancesReport({
      pageIndex: 1,
      pageSize: 1,
      queryDataBaseFiled: 'goodsName',
      likeWords: good.goodsName,
      beginTime: dataInfo.search.beginTime,
      endTime: dataInfo.search.endTime,
      categoryCode: '',
      resultFilter: [],
    })
      .then((res: any) => {
        dataInfo.closingBalance = res.list.find((el: any) => el.goodsName === good.goodsName && el.specificationName === good.specificationName);
      })
      .catch(() => {});
  };
  // 获取库存数据
  const getGetPageStockGoodsLog = (good: any) => {
    proxy.$api.StockGoods.GetPageStockGoodsLog({
      pageIndex: 1,
      pageSize: 9999,
      totalCount: 3,
      queryDataBaseFiled: '',
      likeWords: '',
      beginTime: '2022-01-01',
      endTime: dataInfo.search.endTime,
      onlySelf: 0,
      isShowDisabled: -1,
      orderByFiled: '',
      orderByType: 0,
      goodsCode: '',
      supplierCode: '',
      warehouseCode: '',
      warehouseAreaCode: '',
      areaShelfCode: '',
      isDisable: 0,
      specificationCode: good.specificationCode,
      stockGoodsCode: '',
    })
      .then((res: any) => {
        dataInfo.table.tbody = res.list.map((el: any) => {
          const _changeQuantity = el.changeType.includes('入库') ? Number(el.changeQuantiy) : -Number(el.changeQuantiy);
          return {
            ...el,
            productionDate: proxy.$day(el.productionDate),
            changeQuantity: _changeQuantity,
            afterStockQuantity1: proxy.$count(`${el.afterStockQuantity || 0}-${_changeQuantity || 0}`),
          };
        });
      })
      .catch(() => {});
  };
  // 获取表数据
  const getTbody = () => {
    selectGoodBtn([
      {
        specificationCode: 'GG333596',
      },
    ]);
  };
  // 导出数据
  const exportBtn = (tbody: any[]) => {
    proxy.$exportData(
      {
        thead: dataInfo.table.thead,
        tbody: tbody,
        fileName: dataInfo.table.title,
      },
      dataInfo,
    );
    dataInfo.loading = false;
  };
  const setLoading = (title: string = '正在获取数据,请稍等...') => {
    if (!dataInfo.loading) {
      dataInfo.loading = true;
      dataInfo.loadingMsg = title;
    }
  };
  // 组件挂载后执行
  onMounted(() => {
    getTbody();
  });
  // 组件卸载前执行
  onBeforeUnmount(() => {});
  return { dataInfo, getTbody, getList, selectGoodBtn };
}
